package cn.edu.uestc.topo;

import java.util.UUID;

import org.json.JSONObject;

public class Stat {

    private UUID id = null;
    private UUID tunnelId = null;
    private int priority = 0;

    private long sequenceNumber = 0L;
    private long meterFlow = 0L;
    private int meterDelay = 0;
    private int meterPacketLoss = 0;

    public Stat() {

    }

    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }

    public UUID getTunnelId() {
        return tunnelId;
    }

    public void setTunnelId(UUID tunnelId) {
        this.tunnelId = tunnelId;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public long getSequenceNumber() {
        return sequenceNumber;
    }

    public void setSequenceNumber(long sequenceNumber) {
        this.sequenceNumber = sequenceNumber;
    }

    public long getMeterFlow() {
        return meterFlow;
    }

    public void setMeterFlow(long meterFlow) {
        this.meterFlow = meterFlow;
    }

    public int getMeterDelay() {
        return meterDelay;
    }

    public void setMeterDelay(int meterDelay) {
        this.meterDelay = meterDelay;
    }

    public int getMeterPacketLoss() {
        return meterPacketLoss;
    }

    public void setMeterPacketLoss(int meterPacketLoss) {
        this.meterPacketLoss = meterPacketLoss;
    }

    public void readFromJson(JSONObject jsonObject) {

        try {
            this.id = UUID.fromString(jsonObject.getString("id"));
            this.tunnelId = UUID.fromString(jsonObject.getString("tunnelInfoId"));
            this.priority = jsonObject.getInt("priority");
            this.sequenceNumber = jsonObject.getLong("sequenceNumber");
            this.meterFlow = jsonObject.getLong("meterFlow");
            this.meterDelay = jsonObject.getInt("meterDelay");
            this.meterPacketLoss = jsonObject.getInt("meterPacketLoss");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public JSONObject writeToJson() {

        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("id", this.id.toString());
            jsonObject.put("tunnelInfoId", this.tunnelId.toString());
            jsonObject.put("priority", this.priority);
            jsonObject.put("sequenceNumber", this.sequenceNumber);
            jsonObject.put("meterFlow", this.meterFlow);
            jsonObject.put("meterDelay", this.meterDelay);
            jsonObject.put("meterPacketLoss", this.meterPacketLoss);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return jsonObject;
    }

    @Override
    public String toString() {

        String stringContent = null;
        try {
            stringContent = this.writeToJson().toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return stringContent;
    }

}
